package com.test;

import com.ProvierAndCustoer.Bread;
import com.ProvierAndCustoer.Productor;

import java.util.concurrent.TimeUnit;

/**
 * Created by 41579 on 2017/4/20.
 */
public class ProductorRunnable implements Runnable{

    Productor productor;

    public ProductorRunnable(Productor productor) {
        this.productor = productor;
    }

    @Override
    public void run() {
        while (true){
            synchronized (productor.getResources()) {
                if (productor.getResources().get().size() < 10) {
                    Bread bread = new Bread();
                    productor.product(bread);
                    System.out.println(Thread.currentThread().getName()+" add "+productor.getResources().get().size());
                    try {
                        TimeUnit.SECONDS.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        productor.getResources().notifyAll();
                        productor.getResources().wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
